#include "timer_widget.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTimerEvent>

// #define USER_TIMER_EVENT

TimerWidget::TimerWidget(QWidget *parent): QWidget{parent}
{
    // 垂直布局
    QVBoxLayout* verticalLayout = new QVBoxLayout(this);
    verticalLayout->setSpacing(0);
    verticalLayout->setContentsMargins(0,0,0,0);

    // 文本1
    label1 = new QLabel(this);
    label1->setText("");
    label1->setFrameShape(QFrame::Box);
    label1->setFixedSize(100,100);
    label1->setStyleSheet("background-color: red;");

    verticalLayout->addWidget(label1);

    // 文本2
    label2 = new QLabel(this);
    label2->setText("");
    label2->setFrameShape(QFrame::Box);
    label2->setFixedSize(100,100);
    label2->setStyleSheet("background-color: blue;");

    verticalLayout->addWidget(label2);

    QHBoxLayout* horizontalLayout = new QHBoxLayout(this);
    verticalLayout->setSpacing(0);
    verticalLayout->setContentsMargins(0,0,0,0);

    QPushButton* btnStart = new QPushButton(this);
    btnStart->setText("开始");
    horizontalLayout->addWidget(btnStart);

    QPushButton* btnStop = new QPushButton(this);
    btnStop->setText("停止");
    horizontalLayout->addWidget(btnStop);

    QPushButton* btnReset = new QPushButton(this);
    btnReset->setText("复位");
    horizontalLayout->addWidget(btnReset);

    verticalLayout->addLayout(horizontalLayout);

    // 信号槽
    connect(btnStart, &QPushButton::clicked, this, &TimerWidget::onStartClicked);
    connect(btnStop, &QPushButton::clicked, this, &TimerWidget::onStopClicked);
    connect(btnReset, &QPushButton::clicked, this, &TimerWidget::onResetClicked);

    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    connect(timer1, &QTimer::timeout, this, &TimerWidget::onTimeout1);
    connect(timer2, &QTimer::timeout, this, &TimerWidget::onTimeout2);
}

// 启动定时器
void TimerWidget::onStartClicked()
{
#ifdef USER_TIMER_EVENT
    id1 = startTimer(10);
    id2 = startTimer(20);
#else
    timer1->start(20);
    timer2->start(10);
#endif
}

// 停止定时器
void TimerWidget::onStopClicked()
{
#ifdef USER_TIMER_EVENT
    killTimer(id1);
    killTimer(id2);
#else
    timer1->stop();
    timer2->stop();
#endif
}

// 重置位置
void TimerWidget::onResetClicked()
{
    label1->move(0, label1->y());
    label2->move(0, label2->y());
}


void TimerWidget::onTimeout1()
{
    label1->move(label1->x() + 5, label1->y());
    if(label1->x() > this->width()) {
        label1->move(0, label1->y());
    }
}

void TimerWidget::onTimeout2()
{
    label2->move(label2->x() + 5, label2->y());
    if(label2->x() > this->width()) {
        label2->move(0, label2->y());
    }
}

// 触发的定时器
void TimerWidget::timerEvent(QTimerEvent* event)
{
    if(event->timerId() == id1) {
        label1->move(label1->x() + 5, label1->y());
        if(label1->x() > this->width()) {
            label1->move(0, label1->y());
        }
    }

    if(event->timerId() == id2) {
        label2->move(label2->x() + 5, label2->y());
        if(label2->x() > this->width()) {
            label2->move(0, label2->y());
        }
    }
}
